<?php
function openDb(){
    global $conn;
    
    $dbHost = "localhost";
    $dbUsername = "root";
    $dbPassword = "";
    $dbName = "sribogadb";
    
    $temp = true;
    try {
        $conn = mysql_connect($dbHost,$dbUsername,$dbPassword);
        $db_select = mysql_select_db($dbName);
        if (!$db_select){
            $temp = false;
        }
        closeDB($conn);
    } catch (Exception $e) {
        $temp = false;
    }

    return $temp;
}

function closeDB($conn){
    mysql_close($conn);
}

function dbresult($sql){
    if (openDb()){
        $result = mysql_query($sql) or die (mysql_error()."| ERROR : ".$sql);
        return $result;
    }
}

function cari_nilai($sql){
    $res=dbresult($sql);
    $r=pg_fetch_row($res);
    return $r[0];
}

function Execute($sql){
    try {
        dbresult($sql);
    } catch (Exception $e) {
        throw $e;
    }

}



function dbresultODBC($sql){
    $dbDSN = "sqlserver-local";
    $dbUsername = "sa";
    $dbPassword = "";
    $dbName = "AXSRIBOGA";
    
    try {
        $conn = odbc_connect($dbDSN,$dbUsername,$dbPassword);
        if ($conn){
            $result = odbc_exec($conn,$sql) or die (odbc_error()."| ERROR : ".$sql);
//            closeDbODBC($conn);
        }
    } catch (Exception $e) {
        throw $e;
    }
    return $result;
}

function closeDbODBC($conn){
    odbc_close($conn);
}

function cari_nilaiODBC($sql){
    $res=dbresultODBC($sql);
    $r=odbc_fetch_array($res);
    return $r['nilai'];
}

function ExecuteODBC($sql){
    try {
        dbresultODBC($sql);
    } catch (Exception $e) {
        throw $e;
    }

}

function dbresultODBCSP($sql,$data=array()){
    $dbDSN = "sqlserver-local";
    $dbUsername = "sa";
    $dbPassword = "";
    $dbName = "AXSRIBOGA";
    
    try {
        $conn = odbc_connect($dbDSN,$dbUsername,$dbPassword);
        if ($conn){
            $stmt = odbc_prepare($conn,"{".$sql."}") or die (odbc_error()."| ERROR : ".$sql);
            $result = odbc_execute($stmt,$data);
            if ($result){
                echo "SUKSES";
            }else{
                echo "GAGAL";
            }
        }
    } catch (Exception $e) {
        throw $e;
    }
    return $stmt;
}
function cari_nilaiODBCSP($sql,$data=array()){
    $res=dbresultODBCSP($sql,$data);
    $r= odbc_fetch_row($res);
    return $r[0];
}

//function dbresultODBCPDO($sql){
//    $dbDSN = "sqlserver-local";
//    $dbUsername = "sa";
//    $dbPassword = "";
//    $dbName = "AXSRIBOGA";
//    
//    try {
//               $dbh = new PDO("odbc:".$dbDSN,$dbUsername,$dbPassword);
//               $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
//               $res = $dbh->prepare($sql) or die (odbc_error()."| ERROR : ".$sql);
//               $res->execute();
//               $dbh = null;
//        } catch (PDOException $e) {
//            echo $e->getMessage();
//        }
//        
//    return $res;
//}
function dbresultODBCPDO($sql,$data=""){
    $dbDSN = "sqlserver";
    $dbUsername = "sa";
    $dbPassword = "edam07";
    $dbName = "AXSRIBOGA";
    
    try {
               $dbh = new PDO("odbc:".$dbDSN,$dbUsername,$dbPassword);
               $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
               $res = $dbh->prepare($sql) or die (odbc_error()."| ERROR : ".$sql);
               if ($data!=''){
               foreach ($data as $key => &$val) {
                    $key++;
                    $res->bindParam($key, $val);
               }
               }
               $res->execute();
               $dbh = null;
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
        
    return $res;
}
function execODBCPDO($sql,$data=''){
    try {
        $result = dbresultODBCPDO($sql,$data);
    } catch (PDOException $e) {
        throw $e;
    }
    return $result;
}

function cari_nilaiODBCPDO($sql,$data=''){
    $res = dbresultODBCPDO($sql,$data);
    $r= $res->fetch(PDO::FETCH_NUM);
    return $r[0];
}

function exist($sql,$data='')
{
        $resCek = dbresultODBCPDO($sql,$data);
        $cek = $resCek->fetchColumn();
	return ($cek!=0 || $cek!='') ? true : false;
}
?>